package cc.owoo.godpen.analysis;

/**
 * 文本创建工具
 * Created by nimensei
 * 2022-06-07 下午 08:39
 */
public class TextBuilder {
    private final StringBuilder string = new StringBuilder();// 已添加的字符
    private Space space;// 添加的空白字符

    private enum Space {
        SPACE,// 空格
        ENTER// 回车
    }

    /**
     * 添加字符串
     *
     * @param s 需要添加的字符串
     */
    public void append(String s) {
        for (char c : s.toCharArray())
            append(c);
    }

    /**
     * 添加字符
     *
     * @param c 需要添加的字符
     */
    public void append(char c) {
        if (Character.isWhitespace(c)) {
            if (string.length() == 0)
                return;
            if (c == '\n')
                space = Space.ENTER;
            else if (space == null)
                space = Space.SPACE;
            return;
        }
        if (space != null) {
            string.append(space == Space.ENTER ? '\n' : ' ');
            space = null;
        }
        string.append(c);
    }

    @Override
    public String toString() {
        return string.toString();
    }
}
